<?

require_once '../include/common.inc.php';
if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

getMyParam('typeselect');
getMyParam('select2');

if ($typeselect == 1) {
	//ļ
    $sql = "SELECT `name`,`namezh` FROM `category` WHERE `id` = '$select2'";
    $tmp_ary = $db->get_one($sql);	
	$file_name = str_replace(" ","_",$tmp_ary['name']). ".xls";
	$category_name=array();
	$category_name['Category_name']=$tmp_ary['name'];
	$category_name['Category_namezh']=$tmp_ary['namezh'];
	
   // header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
    header("Content-type:application/vnd.ms-excel");
    header("Accept-Ranges:bytes");
	header("Content-Disposition:attachment;filename=".preg_replace("/<[^>]+>/","",$file_name));

    //ͷ   table heaer 
    foreach ($excel_sql_ary as $k=>$v) {
		
        echo $k . "\t";
    }
	
	/*foreach ($excel_category_column as $k => $v){
		if($k!="Seq")
		echo $k . "\t";	
	}*/
	//sub Sub Category 
	$subcategory_cat=array();	
	$sql = "SELECT `product_id`,`category_id` FROM `product_category`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$subcategory_cat[$rs["product_id"]] = $rs['category_id'];
	}	
		
	//echo $select2."\n";
	//sub Sub Category 
	$subcategory_name=array();
	$subcategory_namezh=array();
	$sql = "SELECT `id`,`name`,`namezh` FROM `category` ";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$subcategory_name[$rs["id"]] = $rs['name'];
		$subcategory_namezh[$rs["id"]] = $rs['namezh'];				
	}	
	

	//Finishes
	$finish_id_name = array();	
	$finish_idzh_name = array();
	$sql = "SELECT `id`, `name`,`namezh` FROM `p_finish_set`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$finish_id_name[$rs["id"]] = $rs['name'];
		$finish_idzh_name[$rs["id"]] = $rs['namezh'];
	}	
	//Setting
	$setting_id_name = array();
	$setting_idzh_name = array();
	$sql = "SELECT `id`, `name`,`namezh` FROM `p_setting`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$setting_id_name[$rs['id']] = $rs["name"];
		$setting_idzh_name[$rs['id']]= $rs["namezh"];
	}	
	//Style
	$style_id_name = array();
	$style_idzh_name = array();
	$sql = "SELECT `id`, `name`,`namezh` FROM `p_style`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$style_id_name[$rs['id']] = $rs["name"];
		$style_idzh_name[$rs['id']] = $rs["namezh"];
		
	}	
	//Collection
	$collection_idzh_name = array();
	$sql = "SELECT `id`, `name`,`namezh` FROM `p_collection`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$collection_id_name[$rs['id']] = $rs["name"];
		$collection_idzh_name[$rs['id']] = $rs["namezh"];
	}


    //cat == category 
    $cat_index_ary = array();

  

   // ȡƷ
    $get_cat_ary = array();
	
	//ƷID
	$have_product_id_ary = array();
	
	$product_cat_ary = array();//Ʒ
	
	
	//ȡĿ¼categorise  IDб: category ҳ id Ϊ$select2   product_category   category 
	
	//当前类的子类
	
	$sub_cat_str = GetCategoryTreeIDS($_categories,$select2,false);
	
	
		// sub category
	$product_category_arr=array();
	$sql = "SELECT `product_id`,`category_id` FROM `product_category` WHERE `category_id` IN (". $sub_cat_str .") ORDER BY product_id";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) 
	{		    
		$product_category_arr[$rs["product_id"]][]= $rs['category_id'];				
	}	


	
	$sql = "SELECT pc.`product_id` as product_id, pc.`category_id` as category_id,c.`name` as name,pc.`seq` as seq FROM `product_category` pc INNER JOIN `category` c ON pc.`category_id` = c.id WHERE pc.`category_id` IN (" . $sub_cat_str . ") ORDER BY pc.product_id";

 $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
		
        $get_cat_ary[$rs['product_id']][$rs['category_id']] = $rs['seq'];
        $have_product_id_ary[] = $rs['product_id'];
		$product_cat_ary[$rs['product_id']][] = array("id"=>$rs['category_id'],"category" =>$rs['name'],"seq" =>$rs['seq']);
    }
	
     
	
    $have_product_id_str = implode(",", $have_product_id_ary);
    //$have_product_id_str = 4;
	


	$sql = "SELECT p.id as product_id, p.*,pf.id as finish_id,ps.id as setting_id,psl.id as style_id,pcl.id as collection_id FROM `product` p LEFT OUTER JOIN `p_finish_set` pf ON p.finish_id = pf.id"
		." LEFT OUTER JOIN `p_setting` as ps ON p.setting_id = ps.id"
		." LEFT OUTER JOIN `p_style` as psl ON p.style_id = psl.id"
		." LEFT OUTER JOIN `p_collection` as pcl ON p.collection_id = pcl.id WHERE p.`id` IN (" . $have_product_id_str . ")    ORDER BY p.`code` DESC";
//	echo $sql;exit();


    $query = $db->query($sql);
	
	$strsub="";	
    while($rs = $db->fetch_array($query)) {
       	echo "\n";	
		
		
       /* foreach ($excel_sql_ary as $k=>$v) {	
				
            $rs[$v] = htmlentities($rs[$v]);
            if ($v == "hidden_flag") {
                $rs[$v] = $rs[$v] == 1 ? "N" : "Y";
            } elseif ($v == "new_flag") {
                $rs[$v] = $rs[$v] == 1 ? "Y" : "";
			} elseif ($v == "name") {
				
				//$name = $rs['name'];
				//$name = html_entity_decode($name);
				//echo "<hr>" . $name;exit;				
				//$rs[$v] = html_entity_decode($rs[$v]);
				
			} elseif ($v == "finish_id" || $v == "setting_id" || $v == "style_id" || $v == "collection_id") {
				$aryName = $v.'_name';
				$rs[$v] = $rs[$v]<= 0 ? "" : ${$aryName}[$rs[$v]];
			
			} elseif ($v == "finish_idzh" || $v == "setting_idzh" || $v == "style_idzh" || $v == "collection_idzh") {
				$aryName = $v.'_name';
				$rs[$v] = $rs[$v]<= 0 ? "" : ${$aryName}[$rs[$v]];
			}elseif($v == "Category_name") 
			{
				$rs[$v]=$category_name['Category_name'];
					
			}
			elseif($v == "Category_namezh") 
			{
				$rs[$v]=$category_name['Category_namezh'];
				
			}
            echo $rs[$v] . "\t";
        }*/

		//ʾ
		/*if(isset($product_cat_ary[$rs['id']]) && count($product_cat_ary[$rs['id']])>0){				
			foreach($product_cat_ary[$rs['id']] as $kk => $vv){
				if($kk>0){
					echo "\n";
					foreach ($excel_sql_ary as $k1=>$v1) {
						echo "\t";	
					}
				}
				
				echo $vv['category']. "\t";
				//echo $vv['id']." ".$vv['category']. "\t";
				//echo $vv['seq']. "\t";
			}
		}//if end */
		
		$str="";
		$strsub="";
		$str.=$rs['code']."\t";		
		$str.=$rs['name']."\t";
		//web show
		if($rs['hidden_flag']==1)
		 {
			$str.='N'."\t";
		}else
			{$str.='Y'."\t";}
			// is news 
		if($rs['new_flag']==1){
			$str.='Y'."\t";
		}else
			{$str.='N'."\t";}		
		$str.=htmlentities($rs['comment'])."\t";
		$str.=$rs['pic']."\t";
		$str.=$rs['small_pic']."\t";
		//price
		$str.=htmlentities($rs['price_2'])."\t";
		$str.=htmlentities($rs['price_1'])."\t";
		$str.=htmlentities($rs['price_3'])."\t";
		
		$str.=htmlentities($rs['length'])."\t";
		$str.=htmlentities($rs['width'])."\t";
		$str.=htmlentities($rs['height'])."\t";
		$str.=htmlentities($rs['cbm'])."\t";
		$str.=htmlentities($rs['width2'])."\t";
		$str.=htmlentities($rs['length2'])."\t";
		$str.=htmlentities($rs['height2'])."\t";
		$str.=htmlentities($rs['cuft'])."\t";
		$str.=$rs['finishshown']."\t";
		$str.=$rs['fabricshown']."\t";
		$str.=$rs['leathershown']."\t";
		$str.=$rs['marbleshown']."\t";
		//finish 		
		$str.=$finish_id_name[$rs['finish_id']]."\t";
		//setting
		$str.=$setting_id_name[$rs['setting_id']]."\t";
		$str.=$style_id_name[$rs['style_id']]."\t";
		$str.=$collection_id_name[$rs['collection_id']]."\t";
		$str.=htmlentities($rs['info'])."\t";
		//$str.=$category_name['Category_name']."\t";//Category_id
		
		//取出当前 产品的 所以类别，去掉重复的
		$product_category_arr[$rs['product_id']]=array_unique($product_category_arr[$rs['product_id']]);
		
		if(count($product_category_arr[$rs['product_id']])>0)
		{		 
		  foreach($product_category_arr[$rs['product_id']] as $kk=>$vs)
		  {
		   
		   // if(array_key_exists($vs,$subcategory_name))
		   if($vs!=" "&&array_key_exists($vs,$subcategory_name))		  
			{
			   //$str.=$subcategory_name[$vs].$kk;
			   // $str.=$subcategory_name[$vs].$kk;
			if($kk==0)
			  {
			    $str.=$subcategory_name[$vs];	
			  }else  if($kk > 0)
			  {				 
			  $strsub.="\n";			 
			  for($i=0;$i<27;$i++)
				{
				    $strsub.="\t";
				}
				 $strsub.=$subcategory_name[$vs]."\t\t\t\t";
				 $strsub.=Iconvstr($subcategory_namezh[$vs]).$vs;
				for($i=0;$i<5;$i++)
				{
				   $strsub.="\t";
				} 			  
			  }
			  
			  		  			  
			}
		  }
		  $str.="\t";
		}
		else //空值
		{
		   $str.=" "."\t";
	 }
		
		
		//$str.=$subcategory_name[$subcategory_cat[$rs['product_id']]]."\t";//sub_id
		
		
		
		$str.=Iconvstr($rs['namezh'])."\t";
		$str.=Iconvstr($rs['commentzh'])."\t";
		
		$substrc=Iconvstr($category_name['Category_namezh']);
		$str.=$substrc."\t";//Category_idzh
		//$str.=$category_name['Category_namezh']."\t";//Category_idzh
		
		
		$substr=Iconvstr($subcategory_namezh[$subcategory_cat[$rs['product_id']]]);
		$str.=$substr."\t";//sub_id//sub_id chinese	
		//$str.=$subcategory_namezh[$subcategory_cat[$rs['product_id']]]."\t";//sub_id//sub_id chinese
		
		$str.=Iconvstr($setting_idzh_name[$rs['setting_id']])."\t";
		
		$str.=Iconvstr($style_idzh_name[$rs['style_id']])."\t";
		$str.=Iconvstr($rs['leathershownzh'])."\t";
		$str.=Iconvstr($rs['marbleshownzh'])."\t";
		$str.=Iconvstr($rs['infozh'])."\t";
		//$str.=$rs['new_flag'];
		$str.=$strsub;
		echo $str;		
		
    }	//while end
    exit;
} elseif ($typeselect == 2) {
    $sql = "SELECT `name`, `type` FROM `inventory` WHERE `id` = '$select2'";
    $tmp_ary = $db->get_one($sql);
	$file_name =  "Inventory_" .str_replace(" ","_",$tmp_ary['name']). ".xls";
    //$file_name = "Inventory_" . $tmp_ary['name'] . ".xls";
	//header("Content-type:application/vnd.ms-excel");
	//header("Accept-Ranges:bytes");
	//header("Content-Disposition:attachment;filename=".$file_name);
	
    $inventory_type = $tmp_ary['type'];
    $inventory_name = $tmp_ary['name'];
		
	header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
	//header("Content-type:application/vnd.ms-excel");
	header("Accept-Ranges:bytes");
	header("Content-Disposition:attachment;filename=".preg_replace("/<[^>]+>/","",$file_name));
	
	//ͷ   table heaer
	

    if ($inventory_type == 1) {
        echo "Item#\tQty\tSpecial Price\tSpecial Price2\tLocation";
    } else {
        echo "Item#\tQty\tSpecial Price\tLocation";
    }

    $sql = "SELECT `product_id`, `inventory_id`, `qty`, `special_price`, `special_price2` FROM `product_inventory` WHERE `inventory_id`='$select2'";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $inventory_info_ary[$rs['product_id']] = $rs;
        $product_id_ary[] = $rs['product_id'];
    }

    $product_id_ary = array_unique($product_id_ary);
    $product_id_str = implode(",", $product_id_ary);
    $sql = "SELECT * FROM `product` WHERE `id` IN (" . $product_id_str . ") ORDER BY `code` DESC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        echo "\n";
        echo $rs['code'] . "\t";
        echo $inventory_info_ary[$rs['id']]['qty'] . "\t";
        echo $inventory_info_ary[$rs['id']]['special_price'] . "\t";
        if ($inventory_type == 1) {
            echo $inventory_info_ary[$rs['id']]['special_price2'] . "\t";
        }
        echo $inventory_name . "\t";
    }

    exit;

}

?> 